﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;

namespace KPIS.GERP.GHRMS.DAL
{
    public sealed class RoyalItemRepayDetailDAL : IRoyalItemRepayDetailDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public RoyalItemRepayDetailDAL()
        {
        }
        public IList<RoyalItemRepayInfo> ListRoyalItemRepayDetails(RoyalItemRepayInfo riInfo)
        {
            string strSQL;
            int ParentSeq = riInfo.Parent == null ? 0 : riInfo.Parent.Sequence;

            List<RoyalItemRepayInfo> infoList = new List<RoyalItemRepayInfo>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@Seq", riInfo.Sequence);
                dbManager.AddParameters(2, "@RepaySeq", ParentSeq);

                strSQL = "SELECT rd.REPAY_DTLS_SEQ, rd.REPAY_SEQ, rd.ROYAL_ITEM_SEQ, rd.GENDER_SEQ"
                        + ", rd.AMOUNT, rd.REMARKS, rd.RECORD_STATUS, rd.CREATED_BY, rd.UPDATED_BY"
                    + " FROM HRM_PS_ROYAL_ITEM_REPAY_DTLS rd"
                    + " WHERE rd.RECORD_STATUS not like @DeleteRecord"
                    + (riInfo.Sequence > 0 ? " AND rd.ROYAL_ITEM_SEQ = @Seq" : "")
                    + (ParentSeq > 0 ? " AND rd.REPAY_SEQ = @RepaySeq" : "")
                    + " ORDER BY rd.REPAY_DTLS_SEQ";

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    RoyalItemRepayDAL repayDAL = new RoyalItemRepayDAL();
                    RoyalItemRepayInfo parentInfo = new RoyalItemRepayInfo();
                    if (!rdr.IsDBNull(1))
                    {
                        IList<RoyalItemRepayInfo> parentList = repayDAL.ListRoyalItemRepays(new RoyalItemRepayInfo(rdr.GetInt32(1)));
                        parentInfo = parentList[0];
                    }

                    RoyalItemDAL itemDAL = new RoyalItemDAL();
                    RoyalItemInfo itemInfo = new RoyalItemInfo();
                    if (!rdr.IsDBNull(2))
                    {
                        IList<RoyalItemInfo> itemList = itemDAL.ListRoyalItems(new RoyalItemInfo(rdr.GetInt32(2)));
                        itemInfo =itemList[0];
                    }

                    MasGenderDAL gdDAL = new MasGenderDAL();
                    MasGenderInfo gdInfo = new MasGenderInfo();
                    if (!rdr.IsDBNull(3))
                    {
                        IList<MasGenderInfo> gdList = gdDAL.ListMasGenders(new MasGenderInfo(rdr.GetInt32(3)));
                        gdInfo = gdList[0];
                    }

                    RoyalItemRepayInfo Info = new RoyalItemRepayInfo(rdr.GetInt32(0)
                            , parentInfo, itemInfo, gdInfo
                            , rdr.IsDBNull(4) ? (decimal?)null : (decimal?)rdr.GetDouble(4)
                            , rdr.IsDBNull(5) ? "" : rdr.GetString(5)
                            , rdr.IsDBNull(6) ? "" : rdr.GetString(6)
                            , rdr.IsDBNull(7) ? null : (int?)rdr.GetInt32(7)
                            , rdr.IsDBNull(8) ? null : (int?)rdr.GetInt32(8));
                    infoList.Add(Info);
                }

                return infoList;
            }
            catch
            {
                return null;
                throw;
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
        }    
        
        public void Insert(RoyalItemRepayInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(8);

                dbManager.AddParameters(0, "@RepaySeq", Info.Parent.Sequence);
                dbManager.AddParameters(1, "@RoyalItemSeq", Info.RoyalItems.Sequence);
                dbManager.AddParameters(2, "@GenderSeq", Info.Gender == null ? (int?)null : Info.Gender.Sequence);
                dbManager.AddParameters(3, "@Amount", Info.Amount);
                dbManager.AddParameters(4, "@Remarks", Info.Remarks);
                dbManager.AddParameters(5, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(6, "@CreatedBy", Info.CreatedBy);
                dbManager.AddParameters(7, "@CreatedWhen", DateTime.Today);

                strSQL = "INSERT INTO HRM_PS_ROYAL_ITEM_REPAY_DTLS(REPAY_SEQ, ROYAL_ITEM_SEQ, GENDER_SEQ"
                        + ", AMOUNT, REMARKS, RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " VALUES(@RepaySeq, @RoyalItemSeq, @GenderSeq, @Amount, @Remarks"
                        + ", @RecordStatus, @CreatedBy, @CreatedWhen)";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(RoyalItemRepayInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(8);

                dbManager.AddParameters(0, "@RepayDtlsSeq", Info.Sequence);
                dbManager.AddParameters(1, "@RepaySeq", Info.Parent.Sequence);
                dbManager.AddParameters(2, "@RoyalItemSeq", Info.RoyalItems.Sequence);
                dbManager.AddParameters(3, "@GenderSeq", Info.Gender == null ? (int?)null : Info.Gender.Sequence);
                dbManager.AddParameters(4, "@Amount", Info.Amount);
                dbManager.AddParameters(5, "@Remarks", Info.Remarks);
                dbManager.AddParameters(6, "@UpdatedBy", Info.UpdatedBy);
                dbManager.AddParameters(7, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE HRM_PS_ROYAL_ITEM_REPAY_DTLS"
                    + " SET REPAY_SEQ = @RepaySeq"
                        + ", ROYAL_ITEM_SEQ = @RoyalItemSeq"
                        + ", GENDER_SEQ = @GenderSeq"
                        + ", AMOUNT = @Amount"
                        + ", REMARKS = @Remarks"
                        + ", UPDATED_BY = @UpdatedBy"
                        + ", UPDATED_WHEN = @UpdatedWhen"
                    + " WHERE REPAY_DTLS_SEQ = @RepayDtlsSeq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Delete(int Sequence, int UpdatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@RepayDtlsSeq", Sequence);
                dbManager.AddParameters(1, "@RecordStatus", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@UpdatedBy", UpdatedBy);
                dbManager.AddParameters(3, "@UpdateWhen", DateTime.Today);

                strSQL = "UPDATE HRM_PS_ROYAL_ITEM_REPAY_DTLS"
                    + " SET RECORD_STATUS = @RecordStatus"
                        + ", UPDATED_BY = @UpdatedBy"
                        + ", UPDATED_WHEN = @UpdateWhen"
                    + " WHERE REPAY_DTLS_SEQ = @RepayDtlsSeq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
